package com.bj.common.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bj.users.pojo.AAdmins;
import com.bj.users.service.IUsersService;

/**
 * 未登录前的操作
 * 
 * @author pengbinxuan
 *
 */
@Controller
public class NoLoginController extends BaseController
{
	// 持有一个service
	@Resource
	private IUsersService usersService;

	/**
	 * 管理员登录操作
	 * 
	 * @return
	 */
	@RequestMapping("/adminLogin.htm")
	public String adminLogin()
	{
		return "back/adminLogin";
	}

	/**
	 * 管理员登录提交操作
	 * 
	 * @return
	 */
	@RequestMapping("/adminLoginSubmit.htm")
	public String adminLoginSubmit(String email, String password, HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		/*
		 * 登录成功 失败
		 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("email", email);
		// 这里暂时先用明文代替，后面用MD5替换
		// 查出一条管理员的信息
		AAdmins admin = this.usersService.findOneAdminService(condMap);
		
		// 查出的数据进行登录判断
		if (admin != null)
		{
			if (password.equalsIgnoreCase(admin.getPassword()))
			{
				if (admin.getStatus() == 1)
				{
					session.setAttribute("admin", admin);
					session.setAttribute("lastLoginTime", admin.getLastLoginTime());
					admin.setLastLoginTime(new Date());
					// 跳转到登录界面
					return "redirect:/back/admin/index.htm";
				} else
				{
					this.info = "该用户已经被冻结";
				}
			} else
			{
				this.info = "当前用户密码出现错误";
			}
			request.setAttribute("admin", admin);
		} else
		{
			this.info = "没有该用户";
		}
		request.setAttribute("info", info);
		return this.adminLogin();
	}
}
